//TESH.scrollpos=0
//TESH.alwaysfold=0
<?local slk = require 'slk' ?>
library Database initializer Init

    globals
        private hashtable EntityDatabase=InitHashtable()
        private hashtable ConfigDatabase=InitHashtable()
    endglobals

    // 保存数据与设置数据
    //! textmacro DataHandle takes funcName, name, type
    function Get$funcName$ takes string sort, integer parentKey, string tag returns $type$
        local hashtable table
        if sort == "entity" then
            set table = EntityDatabase
        elseif sort == "config" then
            set table = ConfigDatabase
        endif
        return Load$name$(table, parentKey, StringHash(tag))
    endfunction

    function Set$funcName$ takes string sort, integer parentKey, string tag, $type$ value returns nothing
        local hashtable table
        if sort == "entity" then
            set table = EntityDatabase
        elseif sort == "config" then
            set table = ConfigDatabase
        endif
        call Save$name$(table, parentKey, StringHash(tag),value)
    endfunction
    //! endtextmacro
    //! runtextmacro DataHandle("Int","Integer" ,"integer")
    //! runtextmacro DataHandle("Real","Real" ,"real")
    //! runtextmacro DataHandle("Bool","Boolean" ,"boolean")
    //! runtextmacro DataHandle("Str","Str" ,"string")

    private function Init takes nothing returns nothing
    <?for uid,uobj in pairs(slk.unit) do
        local primary = uobj.Primary
        if primary~=nil and primary~= "" then?>
        call SetStr("config",'<?=uid?>',"Primary","<?=primary?>")
        <?end
    end?>
    endfunction
endlibrary